﻿@{
    ViewBag.Title = "Window";
    Layout = "~/Areas/Admin/Views/Shared/AdminLayout.cshtml";
}
<script src="/Scripts/ajaxfileupload.js"></script>

<input name="id" id="id" type="hidden" value="0" />
<table style="margin-left: 10px; font-size: 12px;">
    <tr>
        <td><label>商品分类</label></td>
        <td><input type="text" id="sort" name="sort" style="width: 300px;" /></td>
    </tr>
    <tr>
        <td><label>描述</label></td>
        <td><input type="text" id="des" style="width: 300px;" /></td>
    </tr>
    <tr>
        <td><label>分类图片</label></td>
        <td>
            <input type="hidden" id="img" value="0"/>
            <img src="/Areas/Admin/style/images/200 200.jpg" id="zsimg" style="border: 1px red dashed; width: 200px;height: 200px;" onclick="checkfile()" />
            <input id="fileToUpload" style="display: none;" type="file" onchange="upload()" name="upfile">
        </td>
    </tr>
    <tr>
        <td><label>序号</label></td>
        <td><input type="text" id="order" style="width: 100px;" /></td>
    </tr>
    <tr>
        <td colspan="2">
            <div class="botton" style="margin-left: 50px;" onclick="edit()">保存</div>
            <div class="botton" style="margin-left: 50px;" onclick="history.go(-1)">取消</div>
        </td>
    </tr>
</table>
<div style="width: 1000px; height: 40px; margin-top: 200px; "></div>

<script>
    var id = "@ViewData["id"]";
    if (parseFloat(id) > 0) {
        $.post("@Url.Action("GetOnetData")", { id: id }, function(data) {
            var obj = JSON.parse(data);
            $("#id").val(obj[0].Id);
            $("#sort").val(obj[0].Name);
            $("#des").val(obj[0].Des);
            $("#img").val(obj[0].Img);
            $("#zsimg").attr("src",obj[0].Img);
            $("#order").val(obj[0].Order);
        });
    }

    function edit() {
        var sort = $("#sort").val();
        if (sort.trim() == "") {
            alert("请输入分类！");
            return false;
        }
        var des = $("#des").val();
        if (des.trim() == "") {
            alert("请输入描述！");
            return false;
        } else {
            if (des.length > 30) {
                alert("描述只能输入30个字！");
                return false;
            }
        }
        var order = $("#order").val();
        if (order == "") {
            alert("请输入序号！");
            return false;
        }
        var reg = /^[0-9]*[1-9][0-9]*$/;
        if (!reg.test(order)) {
            alert("序号请输入正整数！");
            return false;
        }
        var img = $("#img").val();
        if (img == 0) {
            alert("请上传图片！");
            return false;
        }
        $.post("@Url.Action("Edit")", { id: id, sort: sort, order: order,des:des,img:img }, function(row) {
            if (row > 0) {
                alert("操作成功！");

            } else {
                alert("添加失败！");
            }
            location.href = "@Url.Action("Index")";

        });
    }



    function checkfile() {
        $('#fileToUpload').click();
    }
    function upload() {
        var val = $("#fileToUpload").val();
        if (val == "") {
            alert("请上传图片！");
            return false;
        }
        var suffix = val.split('.');
        var arr = new Array("png", "jpg", "gif");
        if (!Array.prototype.indexOf) {

            Array.prototype.indexOf = function (val) {
                var value = this;
                for (var i = 0; i < value.length; i++) {
                    if (value[i] == val) return i;
                }
                return -1;
            };
        }
        if (arr.indexOf(suffix[suffix.length - 1]) < 0) {
            alert("上传格式有误！");
        } else {
            $.ajaxFileUpload({
                url: '@Url.Action("UploadImg")',
                secureuri: false,
                fileElementId: 'fileToUpload',
                dataType: 'json',
                success: function (data) {
                    if (data.status == 1) {
                        $("#zsimg").attr("src", data.path);
                        $("#img").val(data.path);
                    } else {

                    }
                }
            });
        }
    }
</script>

